package cn.mxz.robots.province;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import net.sf.json.JSONObject;
import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 海南省
 * 排名:完成
 * 去年排名:完成
 * 
 * @author chenmin<chenmin@mouge.wang>
 * @since 2015-06-29
 */
public class Hainan extends Exam implements IRank
{

	public Hainan(Student student)
	{
		super(student);
	}

	public static void main(String[] args)
	{
		Student student = new Student();
		student.setStudentId("01502728");
		student.setStudentPwd("h48b644m");
		student.setType("bl");
		Hainan guangxi = new Hainan(student);
		System.out.println(guangxi.login(""));
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://202.100.202.19/gkbm2015/gkcj_search.php", mSha1);
		loginRequest.addHeader("Referer", "http://202.100.202.19/gkcj_2015.php");
		loginRequest.addParams("pass", mStudent.getStudentId());
		loginRequest.addParams("KSH", mStudent.getStudentId());
		loginRequest.addParams("pass1", mStudent.getStudentPwd());
		loginRequest.addParams("x", "36");
		loginRequest.addParams("y", "28");
		String loginHtml = curlPost(loginRequest);

		JSONObject resultObject = new JSONObject();
		if (loginHtml.contains("语文"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("您输入的卡号或者密码有误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document resultDoc = Jsoup.parse(parseHtml);
		Elements tables = resultDoc.select("table");

		JSONObject json = new JSONObject();
		json.put("exam_id", tables.get(3).select("tr").get(3).select("td").get(1).text());
		json.put("name", regexMatch(tables.get(2).select("tr").get(0).select("td").get(0).text(), "(.*)  考"));
		json.put("ranking", getRank(40, tables.get(7).select("tr").get(1).select("td").get(6).text(), mStudent.getType()));
		json.put("province", "海南省");

		JSONObject score = new JSONObject();
		score.put("语文", tables.get(7).select("tr").get(1).select("td").get(0).text());
		score.put("数学", tables.get(7).select("tr").get(1).select("td").get(1).text());
		score.put("英语", tables.get(7).select("tr").get(1).select("td").get(2).text());
		score.put("物理", tables.get(7).select("tr").get(1).select("td").get(3).text());
		score.put("化学", tables.get(7).select("tr").get(1).select("td").get(4).text());
		score.put("生物", tables.get(7).select("tr").get(1).select("td").get(5).text());
		score.put("综合分", tables.get(7).select("tr").get(1).select("td").get(6).text());
		score.put("政治", tables.get(11).select("tr").get(1).select("td").get(0).text());
		score.put("历史", tables.get(11).select("tr").get(1).select("td").get(1).text());
		score.put("地理", tables.get(11).select("tr").get(1).select("td").get(2).text());
		score.put("技术", tables.get(11).select("tr").get(1).select("td").get(3).text());
		score.put("总分", tables.get(11).select("tr").get(1).select("td").get(4).text());
		score.put("总分的10%", tables.get(11).select("tr").get(1).select("td").get(5).text());
		json.put("score", score.toString());

		return json.toString();
	}

	@Override
	public void initRank()
	{
		// 文 http://202.100.202.19/phtml/2015/06/26/3613.html
		// 理 http://202.100.202.19/phtml/2015/06/26/3612.html
		Request request = new Request("http://202.100.202.19/phtml/2015/06/26/3612.html", mSha1);
		Document result = Jsoup.parse(curlGet(request));

		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 2; i < trs.size(); i++)
		{
			Integer score = Integer.valueOf(trs.get(i).select("td").get(0).text());
			Integer counter = Integer.valueOf(trs.get(i).select("td").get(1).text());
			Integer ranking = Integer.valueOf(trs.get(i).select("td").get(2).text());
			System.out.println(score + ":" + counter + ":" + ranking);
			
			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(40);
			GaoKaoRankService rankService = new GaoKaoRankService();
			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(score);
			rank.setCount(counter);
			rank.setType("l");
			rank.setRanking(ranking);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);
		}
	}

	@Override
	public void lastYearRank()
	{
		//理 http://www.gxeduw.com/gaokao/2015/124813.html
		//文 http://www.gxeduw.com/gaokao/2015/124814.html
		Request request = new Request("http://www.gxeduw.com/gaokao/2015/124814.html", mSha1);
		Document result = Jsoup.parse(curlGet(request));
		
		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 1; i < trs.size(); i++)
		{
			Integer scores = Integer.parseInt(trs.get(i).select("td").get(0).text());
			Integer counters = Integer.parseInt(trs.get(i).select("td").get(1).text());
			Integer rankings = Integer.parseInt(trs.get(i).select("td").get(2).text());		
			System.out.println(scores + ":" + counters + ":" + rankings);
			
			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(9);
			GaoKaoRankService rankService = new GaoKaoRankService();
			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(scores);
			rank.setCount(counters);
			rank.setType(WEN_KE);
			rank.setRanking(rankings);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);
		}
		
	}
}
